/**
 * 
 */
package co.fitstart.coach.entity.rest;

import java.util.Date;
import java.util.List;

import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;

/**
 * @author downpour
 *
 */
public class RestRule {
    
    private Date today;
    
    private Date startDate;
    
    private List<Date> thisWeek;
    
    private String lastWeek;
    
    private String nextWeek;
    
    /**
     * 
     * @param startDate
     */
    public RestRule(Date startDate) {
        
        // create today anyway
        this.today = new Date();
        
        // calculate startDate
        this.startDate = this.calculateStartDate(startDate);
        
        // calculate thisWeek
        this.thisWeek = this.calculateThisWeek(this.startDate);
        
        // calculate lastWeek
        this.lastWeek = this.calculateLastWeek(this.startDate, this.today);
        
        // calculate nextWeek
        this.nextWeek = this.calculateNextWeek(this.startDate, this.today);
    }
    
    /**
     * weekly rest hours should not big than 37 hours
     * 
     * @param restCalendarDayHour
     * @return
     */
    public boolean isFull(int restCalendarDayHour) {
        return restCalendarDayHour >= 37;
    }

    /**
     * 
     * @param baseDate
     * @return
     */
    public Date calculateStartDate(Date baseDate) {
        this.startDate =  baseDate == null ? DateUtils.getFirstWeekDay(this.today) : baseDate;
        return this.startDate;
    }

    /**
     * 
     * @param startDate
     * @return
     */
    public List<Date> calculateThisWeek(Date startDate) {
        Date monday = DateUtils.getFirstWeekDay(startDate);
        return DateUtils.getIntervalDays(monday, DateUtils.addDays(monday, 6));
    }
    
    /**
     * 
     * @param startDate
     * @param today
     * @return
     */
    public String calculateLastWeek(Date startDate, Date today) {
        
        Date monday = DateUtils.getFirstWeekDay(startDate);
        Date lastWeek = DateUtils.addWeeks(monday, -1);
        
        Date weekFirst = DateUtils.getFirstWeekDay(today);
        
        if(DateUtils.weeksBetween(lastWeek, weekFirst) <= 8) {
            return DateFormatUtils.format(lastWeek);
        }
        
        return null;
    }
    
    /**
     * 
     * @param startDate
     * @param today
     * @return
     */
    public String calculateNextWeek(Date startDate, Date today) {
        
        Date monday = DateUtils.getFirstWeekDay(startDate);
        Date nextWeek = DateUtils.addWeeks(monday, 1);
        
        Date weekFirst = DateUtils.getFirstWeekDay(today);
        
        if(DateUtils.weeksBetween(weekFirst, nextWeek) <4) {
            return DateFormatUtils.format(nextWeek);
        }
        
        return null;
    }

    /**
     * @return the today
     */
    public Date getToday() {
        return this.today;
    }

    /**
     * @return the startDate
     */
    public Date getStartDate() {
        return this.startDate;
    }

    /**
     * @return the thisWeek
     */
    public List<Date> getThisWeek() {
        return this.thisWeek;
    }

    /**
     * @return the lastWeek
     */
    public String getLastWeek() {
        return this.lastWeek;
    }

    /**
     * @return the nextWeek
     */
    public String getNextWeek() {
        return this.nextWeek;
    }
}
